﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;

using ICSharpCode.SharpZipLib.Zip;



namespace Hisun.Controllers
{
    public class AbcZip
    {

        /// <summary>
        /// 
      //      AbcZip zip = new AbcZip();
        //    string[] files=Directory.GetFiles(Server.MapPath("bin"));
          //  zip.ZipFiles(files,@"D:\src\Hisun\ABC\test.zip")
        /// </summary>
        /// <param name="files"></param>
        /// <param name="zipfile"></param>
        /// <returns></returns>
        int m_numFiles = 0;        //文件总数
        List<string> m_pathList = new List<string>(); //文件列表

        void ParseDirectory(string path)
        {
            if (Directory.Exists(path))
            {

                string[] dirs = Directory.GetDirectories(path);//得到子目录
                IEnumerator iter = dirs.GetEnumerator();
                while (iter.MoveNext())
                {
                    string str = (string)(iter.Current); 
                  
                     ParseDirectory(str);                    
                }
                string[] files = Directory.GetFiles(path);
                foreach(string file in files)
                {
                    m_numFiles++;
                    
                    m_pathList.Add(file);
                }
            }
        }

        void GetFiles(string[] files)
        {
            foreach (string file in files)
            {
                if (Directory.Exists(file))
                {
                    ParseDirectory(file);
                }
                else if (File.Exists(file))
                {
                    m_numFiles++;
                    m_pathList.Add(file);
                }           
                

            }

        }

        public bool ZipFiles(string file, string zipfile)
        {
            string[] files = { file };

            return ZipFiles(files, zipfile);

        }

        public bool ZipFiles(List<string> listfiles, string zipfile)
        {
            if (listfiles.Count == 0)
            {
                return false;
            }
            string[] files=new string[listfiles.Count];
            listfiles.CopyTo(files);

            return ZipFiles(files, zipfile);

        }


        public bool ZipFiles(string[] files, string zipfile)
        {
            if(File.Exists(zipfile))
            {
                File.Delete(zipfile);//如果文件存在，则删除

               // return false;//文件存在;

            }
            GetFiles(files);

            try
            { 
                using( ZipOutputStream s=new ZipOutputStream(File.Create(zipfile)))
                {
                    s.SetLevel(2);
                    byte[] buffer= new  byte[1024*8];
                    
                    foreach (string file in m_pathList)
                    {
                        //if (Directory.Exists(file))
                        //{
                        //   // throw new Exception("目前不支持目录");
                             
                        //}
                     
                        //if(File.Exists(file))
                        //{
                            ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                            entry.DateTime = DateTime.Now;                            
                             s.PutNextEntry(entry);
                            using (FileStream fs = File.OpenRead(file))
                            {
                                int sourceBytes;
                                do 
                               {

                                   sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                    s.Write(buffer, 0, sourceBytes);
                               }
                              while ( sourceBytes > 0 );
                            }

                         //} //File exists
                            
                        }//for each

                    s.Finish();
                    s.Close();

                    } //ZipOutputSteam
                } // Try
            catch (Exception ex)
              {

                  throw ex;

                 // return false;

               }


            return true;
        }

       



    }
}
